package TakeAwaySystem.businessoperation.business;

import TakeAwaySystem.Business;
import TakeAwaySystem.businessoperation.BusinessIOperatition;
import TakeAwaySystem.User;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @author Vv
 * @version 1.0
 * @project coding_learn
 * @description
 * @date 2023/9/25 22:53:21
 */
public class QueryBusinessOpertion implements BusinessIOperatition {
    @Override
    public void work(Business o, File file) {
        Business that = (Business) o;
        System.out.println("显示当前登录的商户信息");
        //System.out.println(o.toString());
        List<User> userList = fileToList(file);
        for(User user : userList){
            if(user.getId() == o.getId()){
                System.out.println(((Business) user).toString());
                o.setName(((Business)user).getName());
                o.setBusinessAddress(((Business)user).getBusinessAddress());
                o.setBusinessExplain(((Business)user).getBusinessExplain());
                o.setStarPrice(((Business)user).getStarPrice());
                o.setDeliveryPrice(((Business)user).getDeliveryPrice());
                break;
            }
        }
        ListToFile(userList,file);
        System.out.println("按下回车后回到商户功能菜单");
        Scanner scanner = new Scanner(System.in);
        String str = scanner.nextLine();
    }

    @Override
    public void ListToFile(List<User> list,File file) {
       // File file = new File("d:/Business.txt");
        FileWriter fileWriter = null;
        BufferedWriter bw = null;
        try {
            fileWriter = new FileWriter(file);
            bw = new BufferedWriter(fileWriter);
            int N = list.size();
            for(int i = 0;i<N;i++){
                Business user = (Business) list.get(i);
                StringBuilder sbr = new StringBuilder();
                sbr.append(user.getId() + " ");
                sbr.append(user.getPassword() + " ");
                sbr.append(user.getName() + " ");
                sbr.append(user.getBusinessAddress() + " ");
                sbr.append(user.getBusinessExplain() + " ");
                sbr.append(user.getStarPrice() + " ");
                sbr.append(user.getDeliveryPrice());
                bw.write(sbr.toString());
                bw.newLine();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                bw.close();
                fileWriter.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

        }

    }


    @Override
    public List<User> fileToList(File file)  {
        List<User> list = new ArrayList<>();
       // File file = new File("d:/Business.txt");
        FileReader fr = null;
        BufferedReader br = null;
        try {
            fr = new FileReader(file);
             br = new BufferedReader(fr);
            while(true){
                String str = br.readLine();
                if(str != null){
                    String[] arr = str.split(" ");
                    Business users = new Business();
                    users.setId(Integer.valueOf(arr[0]));
                    users.setPassword(arr[1]);
                    users.setName(arr[2]);
                    users.setBusinessAddress(arr[3]);
                    users.setBusinessExplain(arr[4]);
                    users.setStarPrice(Double.valueOf(arr[5]));
                    users.setDeliveryPrice(Double.valueOf(arr[6]));
                    list.add(users);
                }else{
                    break;
                }
            }
        }catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                fr.close();
                br.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        return list;
    }

}
